<?php
class Admin_CompanyController extends Zendhlt_Controller_AdminAction {
	
	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;

	//Duong dan cua Controller
	protected $_currentController;

	//Duong dan cua Action chinh
	protected $_actionMain;

	//Admin user dang login
	protected $_crrUsra;
	protected $_namespace;

	public function init() {
		$layoutPath = TEMPLATE_PATH  . '/admin';
		$option = array ('layout' => 'index',
                   'layoutPath' => $layoutPath );
		Zend_Layout::startMvc ( $option );
		
		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();

		//Duong dan cua Controller
		$this->_currentController = '/' . $this->_arrParam['module']
		. '/' . $this->_arrParam['controller'];

		//Duong dan cua Action chinh
		$this->_actionMain = '/' . $this->_arrParam['module']
		. '/' . $this->_arrParam['controller'];

		//Truyen ra view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
	}

	public function indexAction() {
		$model = new Admin_Model_ThongTinCtyMapper();
		$thong_tin_cty = $model->fetchAll();
		$this->view->thong_tin_cty = $thong_tin_cty;
		$form = new Admin_Form_Company();
		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			$flag = false;
			if ($form->isValid($formData)) {
				$ttCty = new Admin_Model_TableColumn_ThongTinCty();
				$ttCty->setId('cty_ten');
         		$ttCty->setNoi_dung($formData['cty_ten']);
         		$model->save($ttCty);
				$ttCty->setId('cty_dia_chi');
         		$ttCty->setNoi_dung($formData['cty_dia_chi']);
         		$model->save($ttCty);
				$ttCty->setId('cty_hotline');
         		$ttCty->setNoi_dung($formData['cty_hotline']);
         		$model->save($ttCty);
				$ttCty->setId('cty_nick_skype');
         		$ttCty->setNoi_dung($formData['cty_nick_skype']);
         		$model->save($ttCty);
				$ttCty->setId('cty_nick_yahoo');
         		$ttCty->setNoi_dung($formData['cty_nick_yahoo']);
         		$model->save($ttCty);
				$message = 'Cập nhật thông tin thành công.';
				$flag = true;
			} else {
				$message = 'Xảy ra lỗi trong quá trình sử lý.';
			}
			$this->_helper->viewRenderer->setNoRender();
			$this->_helper->getHelper('layout')->disableLayout();
			header('Content-type: application/json');
			$json = array('edit' => $flag, 'message' => $message);
			echo json_encode($json);
		} else {
			$form->cty_ten->setValue($thong_tin_cty['cty_ten']);
			$form->cty_dia_chi->setValue($thong_tin_cty['cty_dia_chi']);
			$form->cty_hotline->setValue($thong_tin_cty['cty_hotline']);
			$form->cty_nick_skype->setValue($thong_tin_cty['cty_nick_skype']);
			$form->cty_nick_yahoo->setValue($thong_tin_cty['cty_nick_yahoo']);
			$this->view->form = $form;
		}
	}

	public function aboutAction() {
		$info = "cty_gioi_thieu";
		$title = "Giới thiệu";
		if (isset($this->_arrParam['info']) && $this->_arrParam['info'] == 'quydinh') {
			$info = "cty_quy_dinh";
			$title = "Quy định";
		}
		$model = new Admin_Model_ThongTinCtyMapper();
		$thong_tin_cty = $model->fetchAll("id = '" . $info ."'");
		$this->view->id_record = $info;
		$this->view->title = $title;
		$textarea = new Zend_Form_Element_Textarea('noi_dung');
		$textarea->setDecorators(array('ViewHelper'));
		$this->view->textarea = $textarea;
		if ($this->getRequest()->isPost()) {
			$validator = new Zend_Validate();
			$validator->addValidator(new Zend_Validate_NotEmpty(), true);
         	$formData = $this->getRequest()->getPost();
         	if ($validator->isValid($formData['noi_dung'])) {
         		$ttCty = new Admin_Model_TableColumn_ThongTinCty();
         		$ttCty->setId($info);
         		$ttCty->setNoi_dung($formData['noi_dung']);
         		$model->save($ttCty);
				$textarea->setValue($formData['noi_dung']);
         	} else {
         		$this->view->msg = $validator->getMessages();
         	}
		} else {
			$textarea->setValue($thong_tin_cty[$info]);
		}
	}

	// Validate form
	public function validateformAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();

		$f = new Admin_Form_Company();
		$f->isValid($this->_getAllParams());
		$json = $f->getMessages();
		header('Content-type: application/json');
		echo Zend_Json::encode($json);
	}
}